package com.zjdiepu.www.biz.system;

import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.dao.system.BaseBusiUnitMapper;
import com.zjdiepu.www.ibiz.system.IBaseBusiUnitService;
import com.zjdiepu.www.model.system.dto.BaseBusiUnit;
import com.zjdiepu.www.model.system.ext.BaseBusiUnitExt;

/**
 * 业务单位Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年07月31日 11时30分<br/>
 * @since 1.0<br/>
 */
@Service
public class BaseBusiUnitServiceImpl extends BaseService implements IBaseBusiUnitService {
	public static final Logger logger = LoggerFactory.getLogger(BaseBusiUnitServiceImpl.class);

	@Autowired
	private BaseBusiUnitMapper baseBusiUnitMapper;
	
   /* 
	* @author zhangpp
	* <p>Title: getServiceByParentId</p> 
	* <p>Description: </p> 
	* @param baseBusiUnitExt
	* @return 
	* @see com.api.system.service.IBaseBusiUnitService#getServiceByParentId(com.api.system.entity.ext.BaseBusiUnitExt) 
	* @version V1.0
	*/
	@Override
	public List<BaseBusiUnit> getServiceByParentId(
			BaseBusiUnitExt baseBusiUnitExt) {
		
		return baseBusiUnitMapper.getByParentId(baseBusiUnitExt);
	}
	
	@Override
	public void saveUpdateAndDelete(BaseBusiUnit baseBusiUnit, OperateType operType) {
		switch (operType) {
		case add:
			baseBusiUnit.initInsertData(baseBusiUnit.getModifier(), baseBusiUnit.getModifierName());
			baseBusiUnitMapper.insert(baseBusiUnit);
			break;
		case edit:
			baseBusiUnit.initUpdateData(baseBusiUnit.getModifier(), baseBusiUnit.getModifierName());
			// 影响的记录数
			int updateCount = baseBusiUnitMapper.updateByPrimaryKeySelective(baseBusiUnit);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			baseBusiUnit.initDeleteData(baseBusiUnit.getModifier(), baseBusiUnit.getModifierName());
			// 影响的记录数
			int deleteCount = baseBusiUnitMapper.updateByPrimaryKeySelective(baseBusiUnit);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<BaseBusiUnit> selectByExt(BaseBusiUnitExt baseBusiUnitExt) {
		return baseBusiUnitMapper.selectByExt(baseBusiUnitExt);
	}
	
	@Override
	public int countByExt(BaseBusiUnitExt baseBusiUnitExt){
		return baseBusiUnitMapper.countByExt(baseBusiUnitExt);
	}
	
	@Override
	public BaseBusiUnit selectById(String id) {
		return baseBusiUnitMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			baseBusiUnitMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(BaseBusiUnit baseBusiUnit) {
		return baseBusiUnitMapper.countRefined(baseBusiUnit);
	}

	@Override
	public BaseBusiUnit selectRefined(BaseBusiUnit baseBusiUnit) {
		return baseBusiUnitMapper.selectRefined(baseBusiUnit);
	}
	
	@Override
	public int deletedRefined(BaseBusiUnit baseBusiUnit) {
		return baseBusiUnitMapper.deletedRefined(baseBusiUnit);
	}

	@Override
	public boolean repeat(BaseBusiUnit baseBusiUnit) {
		int count = countRefined(baseBusiUnit);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(baseBusiUnit.getId())) {
			return true;
		}
		BaseBusiUnit dbItem = selectRefined(baseBusiUnit);
		if(null == dbItem) {
			return false;
		}
		return !baseBusiUnit.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return baseBusiUnitMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<BaseBusiUnit> baseBusiUnits) {
		return baseBusiUnitMapper.insertBatch(baseBusiUnits);
	}
}
